package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.DispatchEntity;
import io.renren.modules.app.service.DispatchService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * @date 2022-04-23 19:38:09
 */
@RestController
@RequestMapping("app/dispatch")
public class DispatchController {
    @Autowired
    private DispatchService dispatchService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:dispatch:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = dispatchService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{dispatchId}")
    @RequiresPermissions("generator:dispatch:info")
    public R info(@PathVariable("dispatchId") Long dispatchId) {
        DispatchEntity dispatch = dispatchService.getById(dispatchId);

        return R.ok().put("dispatch", dispatch);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:dispatch:save")
    public R save(@RequestBody DispatchEntity dispatch) {
        dispatchService.save(dispatch);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:dispatch:update")
    public R update(@RequestBody DispatchEntity dispatch) {
        dispatchService.updateById(dispatch);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:dispatch:delete")
    public R delete(@RequestBody Long[] dispatchIds) {
        dispatchService.removeByIds(Arrays.asList(dispatchIds));

        return R.ok();
    }


    /**
     * 新增派单记录
     *
     * @param dispatch 派单信息类
     * @return
     */
    @Login
    @PostMapping("addDispatch")
    public R addDispatch(@RequestBody DispatchEntity dispatch) {
        Integer integer = dispatchService.addDispatch(dispatch);
        if (integer > 0) {
            return R.ok("派单成功");
        } else {
            return R.error("派单失败");
        }
    }

}
